package com.yunsc.shop.schedule;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yunsc.store.entity.YunscUserCouponEntity;
import com.yunsc.store.service.YunscUserCouponService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @version: 1.0
 * @Author: adan
 * @description: 定时任务，检查并更新过期优惠券状态
 * @date: 2024-05-11 16:24
 */
@Slf4j
@Component
public class CouponExpirationChecker {

    @Autowired
    private YunscUserCouponService userCouponService;

    /**
     * 每天凌晨执行，检查并更新过期优惠券状态
     */
    @Scheduled(cron = "0 0 0 * * ?") // 每天凌晨执行
    public void checkAndUpdateExpiredCoupons() {
        LambdaQueryWrapper<YunscUserCouponEntity> lqw = new LambdaQueryWrapper<>();
        lqw.eq(YunscUserCouponEntity::getIsExpire, 0);
        lqw.eq(YunscUserCouponEntity::getIsUse, 0);
        List<YunscUserCouponEntity> coupons = userCouponService.list(lqw);
        if (coupons.isEmpty()) {
            log.info("今天没有过期优惠券。。。。。");
            return;
        }

        // 更新优惠券状态
        userCouponService.updateCouponExpiration(coupons);

        log.info("今天凌晨检查并更新了过期优惠券状态。。。。。");
    }


    // 0/5 * * * * ?
    // @Scheduled(cron = "0/5 * * * * ?")
    // public void checkAndUpdateExpiredCoupons2() {
    //     log.info("每隔5秒检查并更新过期优惠券状态。。。。。");
    // }


}